<template>
    <div class="article-meta">
        <nuxt-link :to="{
            name:'profile',
            params:{
                username:article.author.username
            }
        }"><img :src="article.author.image" /></nuxt-link>
        <div class="info">
        <nuxt-link 
            :to="{
                name:'profile',
                params:{
                    username:article.author.username
                }
            }" 
            class="author"
        >
            {{article.author.username}}
        </nuxt-link>
        <span class="date">{{article.createdAt | date('MMM DD,YYYY')}}</span>
        </div>
        <button 
            class="btn btn-sm btn-outline-secondary"
            :class="{
                active:article.author.following
            }"
            @click="followingFN(article.author)"
        >
            <i class="ion-plus-round"></i>
            &nbsp;
            Follow {{article.author.username}} <span class="counter"></span>
        </button>
        &nbsp;&nbsp;
        <button 
            class="btn btn-sm btn-outline-primary"
            :class="{
                active:article.author.favorited
            }"
            @click="favoritedFN(article)"
            :disabled="article.favoriteDisabled"
        >
        <i class="ion-heart"></i>
            &nbsp;
            <template v-if="article.favorited">
                Unfavorite Article 
            </template>
            <template v-else>
                Favorite Post
            </template> 
            <span class="counter">({{article.favoritesCount}})</span>
        </button>
    </div>
</template>
<script>
import { addFavorite,deleteFavorite } from '@/api/article'
export default {
    name:'ArticleMeta',
    props:{
        article:{
            type:Object,
            required:true
        }
    },
    methods:{
        // 关注作者
        followingFN(){

        },
        // 喜欢文章
        async favoritedFN(item){
            item.favoriteDisabled = true
            if(item.author.favorited){
                // 取消点赞
                await deleteFavorite(item.slug)
                // item.author.favorited = false
                // item.favoritesCount += -1
            }else{
                await addFavorite(item.slug)
                // item.author.favorited = true
                // item.favoritesCount += 1
            }
            item.favoriteDisabled = false
        }
    }
}
</script>